package com.phiture.erp.basic.web.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import com.phiture.erp.basic.core.pojo.dto.ErpMaterialUnitPageReqDTO;
import com.phiture.erp.basic.core.pojo.dto.ErpMaterialUnitSaveReqDTO;
import com.phiture.erp.basic.core.service.ErpMaterialUnitService;
import com.phiture.erp.basic.dal.entity.ErpMaterialUnitDO;
import com.phiture.erp.basic.core.pojo.vo.ErpMaterialUnitRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

/**
 * @author YH
 * @date 2025/3/28
 */
@RestController
@RequiredArgsConstructor
@Tag(name = "管理后台 - ERP 物料单位")
@RequestMapping("/erp/material-unit")
public class ErpMaterialUnitController {
    private final ErpMaterialUnitService materialUnitService;

    @PostMapping("/create")
    @Operation(summary = "创建物料单位")
    @PreAuthorize("@ss.hasPermission('erp:material-unit:create')")
    public CommonResult<Long> createProductUnit(@Valid @RequestBody ErpMaterialUnitSaveReqDTO createReqDTO) {
        return success(materialUnitService.createMaterialUnit(createReqDTO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料单位")
    @PreAuthorize("@ss.hasPermission('erp:material-unit:update')")
    public CommonResult<Boolean> updateMaterialUnit(@Valid @RequestBody ErpMaterialUnitSaveReqDTO updateReqDTO) {
        materialUnitService.updateMaterialUnit(updateReqDTO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料单位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:material-unit:delete')")
    public CommonResult<Boolean> deleteMaterialUnit(@RequestParam("id") Long id) {
        materialUnitService.deleteMaterialUnit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料单位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:material-unit:query')")
    public CommonResult<ErpMaterialUnitRespVO> getMaterialUnit(@RequestParam("id") Long id) {
        ErpMaterialUnitDO materialUnitDO = materialUnitService.getMaterialUnit(id);
        return success(BeanUtils.toBean(materialUnitDO, ErpMaterialUnitRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料单位分页")
    @PreAuthorize("@ss.hasPermission('erp:material-unit:query')")
    public CommonResult<PageResult<ErpMaterialUnitRespVO>> getMaterialUnitPage(@Valid ErpMaterialUnitPageReqDTO pageReqDTO) {
        PageResult<ErpMaterialUnitDO> pageResult = materialUnitService.getMaterialUnitPage(pageReqDTO);
        return success(BeanUtils.toBean(pageResult, ErpMaterialUnitRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得物料单位精简列表", description = "只包含被开启的单位，主要用于前端的下拉选项")
    public CommonResult<List<ErpMaterialUnitRespVO>> getProductUnitSimpleList() {
        List<ErpMaterialUnitDO> list = materialUnitService.getMaterialUnitListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return success(convertList(list, unit -> new ErpMaterialUnitRespVO().setId(unit.getId()).setName(unit.getName())));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料单位 Excel")
    @PreAuthorize("@ss.hasPermission('erp:material-unit:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductUnitExcel(@Valid ErpMaterialUnitPageReqDTO pageReqDTO, HttpServletResponse response) throws IOException {
        pageReqDTO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpMaterialUnitDO> list = materialUnitService.getMaterialUnitPage(pageReqDTO).getList();
        // 导出 Excel
        ExcelUtils.write(response,
                "物料单位.xls",
                "数据",
                ErpMaterialUnitRespVO.class,
                BeanUtils.toBean(list, ErpMaterialUnitRespVO.class));
    }

}
